package org.iabako.shared.querydefinition.dto;

import org.iabako.shared.entity.enumeration.LabelEnum;
import org.iabako.shared.tools.GenericTools;

import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by jose on 25/03/14.
 */
public class QueryResultValue implements Serializable {

    private QueryResultValueType queryResultValueType;

    private Boolean booleanValue;
    private String stringValue;
    private Double doubleValue;
    private Date dateValue;
    private IconValue iconvalue;
    private LabelEnum enumValue;

    @Transient
    private String dateValueString;

    public QueryResultValueType getQueryResultValueType() {
        return queryResultValueType;
    }

    @Transient
    public Object getValue() {
        if (queryResultValueType == null) {
            return null;
        }
        switch (queryResultValueType) {
            case STRING:
                return stringValue;
            case BOOLEAN:
                return booleanValue;
            case DOUBLE:
                return doubleValue;
            case ENUM:
                return enumValue;
            case DATE:
                return dateValue;
            case ICON:
                return iconvalue;
        }
        return null;
    }

    public void setValue(Object o) {
        if (GenericTools.isEmpty(o)) {
            return;
        }
        if (o instanceof Double) {
            queryResultValueType = QueryResultValueType.DOUBLE;
            doubleValue = (Double) o;
        } else if (o instanceof LabelEnum) {
            queryResultValueType = QueryResultValueType.ENUM;
            enumValue = (LabelEnum) o;
        } else if (o instanceof Date) {
            queryResultValueType = QueryResultValueType.DATE;
            dateValue = (Date) o;
            dateValueString = GenericTools.formatDateToString(dateValue);
        } else if (o instanceof IconValue) {
            queryResultValueType = QueryResultValueType.ICON;
            iconvalue = (IconValue) o;
        } else if (o instanceof Boolean) {
            queryResultValueType = QueryResultValueType.BOOLEAN;
            booleanValue = (Boolean) o;
        } else {
            queryResultValueType = QueryResultValueType.STRING;
            stringValue = o.toString();
        }
    }

    public String getDateValueString() {
        return dateValueString;
    }
}
